﻿//wmts服务
function TileWMTSService(layerInfo) {
    var TileWMTSService = new Object;
    TileWMTSService.id = layerInfo.hasOwnProperty("GUID") ? layerInfo.GUID : "";
    TileWMTSService.url = layerInfo.hasOwnProperty("URL") ? layerInfo.URL : "";
    TileWMTSService.name = layerInfo.hasOwnProperty("NAME") ? layerInfo.NAME : "";
    TileWMTSService.servertype = layerInfo.hasOwnProperty("SERVERTYPE") ? layerInfo.SERVERTYPE : "";
    TileWMTSService.params = layerInfo.hasOwnProperty("PARAMETERS") ? layerInfo.PARAMETERS : "";
    TileWMTSService.gridnames = layerInfo.hasOwnProperty("GRIDNAMES") ? layerInfo.GRIDNAMES : "";
    TileWMTSService.prj = layerInfo.hasOwnProperty("PRJ") ? layerInfo.PRJ : "EPSG:3857";
    TileWMTSService.maxresolution = layerInfo.hasOwnProperty("MAXRESOLUTION") ? layerInfo.MAXRESOLUTION : 20;
    TileWMTSService.style = layerInfo.hasOwnProperty("STYLE") ? layerInfo.STYLE : "";
    TileWMTSService.matrixset = layerInfo.hasOwnProperty("MATRIXSET") ? layerInfo.MATRIXSET : TileWMTSService.prj;
    if (TileWMTSService.gridnames) {
        for (var i = 0 ; i < TileWMTSService.gridnames.length ; i++) {
            TileWMTSService.gridnames[i] = TileWMTSService.gridnames[i].replace(TileWMTSService.prj + ":" , "");
        }
    }

    TileWMTSService.CreateLayer = function () {
        var id = this.id;
        var url = this.url;
        var name = this.name;
        var params = this.params;
        var servertype = this.servertype;
        var prj = this.prj;
        var maxresolution = parseInt(this.maxresolution);
        var matrixset = this.matrixset;
        var style = this.style;
        var gridnames = this.gridnames;
        var prjExtent = ol.proj.get(prj).getExtent();
        var startResolution = ol.extent.getHeight(prjExtent) / 256;
        //var resolutions = new Array(maxresolution);
        //for (var i = 0, ii = resolutions.length; i < ii; ++i) {
        //    resolutions[i] = startResolution / Math.pow(2, i);
        //}
        //var tileGrid = new ol.tilegrid.WMTS({
        //    origin: ol.extent.getTopLeft(prjExtent),
        //    resolutions: resolutions,
        //    tileSize: [256, 256],
        //    matrixIds: gridnames
        //});

        var layers = viewer.scene.imageryLayers;
       // layerindex = layerindex > layers._layers.length ? layers._layers.length : layerindex;
        layerindex = layers._layers.length;

        var provider = new Cesium.WebMapTileServiceImageryProvider({
            url : url,
            layer: params,
            style : style,
            format : 'image/png',
            tileMatrixSetID : 'default028mm',
            maximumLevel: 23,
            tileMatrixLabels: gridnames,
            credit : ''
        });

        var thisLayer = layers.addImageryProvider(provider, layerindex);
        checkLayerTransparent(thisLayer, id);
        return thisLayer;
    }
    return TileWMTSService;
}